<template>
  <card v-bind="cardOptions" @save="save">
    <y-form ref="$form" v-bind="options2"></y-form>
  </card>
</template>

<script>
export default {
  mounted() {
    var { keyValue, parentId } = this.$route.query;
    if (!!keyValue) {
      this.cardOptions.title = "产品 / 编辑";
      this.initData(keyValue);
    } else {
      this.cardOptions.title = "产品 / 新增";
      this.$refs.$form.setData({parentId : (parentId || "0")})
    }
  },
  data() {
    return {
      keyValue: "",
      cardOptions: {
        title: "",
        btns: ["save", "back"],
      },
      options2: {
        items: [
          {
            title: "产品信息",
            fields: [
              { type: "input", field: "code", text: "编号" ,required: true },
              { type: "input", field: "name", text: "名称", required: true },
              { type: "input", field: "subName", text: "副名称", required: true },
              { type: "cascader", field: "parentId", text: "分类",code:'MenuCascaders'},
              // { type: "checkbox", field: "saleable", text: "上架",span:2 },

              { type: "checkbox-group", text: "选项", span:6,
                items:[
                  { field:"saleable",text:"上架" },
                  { field:"saleable2",text:"推荐" },
                  { field:"saleable3",text:"热销" },
                ] 
              },

              { type:"picture-group",field:'imgages',text:"相册",span:24 }
            ],
          },
          {
            title:"SKU信息",
            field:"details",
            height: "300px",
            edit: true,
            columns: [
              { type: "seq", width: 50 },
              { field: "code", title: "编号" },
              { field: "images", title: "图片" },
              { field: "specIndex", title: "规格" },
              { field: "name", title: "名称" },
              { field: "price", title: "价格" },
              { field: "inventory", title: "库存" },
              { field: "warning", title: "库存预警" },
            ],
          },
          {
            title:"描述",
            fields:[
              { type: "input", field: "saleable", text: " ",span:24 },
            ]
          }
        ],
      },
    };
  },
  methods: {
    async initData(keyValue) {
      var { data } = await $y.api.system.menu.get(keyValue);
      this.$refs.$form.setData(data);
    },
    async save() {
      var formData = this.$refs.$form.getFormData();
      if(!formData.id){
        await this.$y.api.system.menu.insert(formData)
      }else{
        await this.$y.api.system.menu.update(formData)
      }
    },
  },
};
</script>

<style lang="scss" scoped>
</style>